package com.lbt.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lbt.eduservice.client.VodClient;
import com.lbt.eduservice.entity.EduVideo;
import com.lbt.eduservice.mapper.EduVideoMapper;
import com.lbt.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-02
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    //注入cLient
    @Autowired
    private VodClient vodClient;
    //还要删除出视频
    @Override
    public void removeByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapperID=new QueryWrapper<>();
        wrapperID.eq("course_id",courseId);
        wrapperID.select("video_source_id");
        List<EduVideo> eduVideIdList = baseMapper.selectList(wrapperID);
       // vodClient.
        List<String>videoIds=new ArrayList<>();
        for(int i=0;i<eduVideIdList.size();i++){
            String videoSourceId = eduVideIdList.get(i).getVideoSourceId();
           if(!StringUtils.isBlank(videoSourceId)) {
               videoIds.add(videoSourceId);
           }
        }
        if(videoIds.size()>0){
            //根据多个视频iD删除视频
            vodClient.deleteBatch(videoIds);
        }
        QueryWrapper<EduVideo> wrapper=new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
